﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ATW.MT.BUS;
using ATW.MT.CommonClass;

namespace ATW.MT.BaseManage
{
    public partial class _editPwd : System.Web.UI.Page
    {
        bus_D_user us = new bus_D_user();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["uid"]))
                {
                    return;
                }
            }
        }
        protected void lbnSave_Click(object sender, EventArgs e)
        {
            string pwd = txtPwd1.Text.Trim();
            string newPwd = txtPwd2.Text.Trim();
            string newPwd2 = txtPwd3.Text.Trim();
            string account = Request.QueryString["uid"];
            try
            {
                if (account == "" || !common.filterChar(account))
                {
                    return;
                }
                if (pwd == "") 
                {
                    Tips("请填写原始密码");
                    return;
                }
                if (!common.filterChar(pwd))
                {
                    Tips("密码中含有危险字符！");
                    return;
                }
                if (!us.checkPwd(account, pwd))
                {
                    Tips("密码错误，请重新输入！");
                    return;
                }
                if (newPwd == "")
                {
                    Tips("新密码不能为空！");
                    return;
                }
                if (!common.filterChar(newPwd))
                {
                    Tips("新密码含有危险字符！");
                    return;
                }
                if (newPwd2 != newPwd)
                {
                    Tips("两次输入密码不一致！");
                    return;
                }
                int result = us.UpdatePwd(account, newPwd);
                if (result > 0)
                {
                    Tips("修改成功！");
                    CloseWindowReferesh();
                }
                else
                {
                    Tips("修改失败！");
                }
            }
            catch (Exception ex)
            {
 
            }


        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }//提示
        private void CloseWindowReferesh()
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                if(window.opener)
                {
                    window.opener.location.href=window.opener.location.href;
                    window.close();
                }
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "Close", strScript.ToString());
        }
    }
}